package com.scenery;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.scenery.services.SceneryServices;

import com.scenery.util.HttpUtil;
import com.scenery.util.Loger;
import com.scenery.view.CityListView;
import com.scenery.view.SceneryListView;
import com.scenery.vo.City;
/**
 * 城市列表
 * 
 * ***/
public class SelectActivity extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	String tag=SelectActivity.class.getName();
	CityListView ctiyListView;
	ArrayList<City> cityList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		 setContentView(R.layout.select);
		 ctiyListView=(CityListView) findViewById(R.id.ctiyList);
		 
		ctiyListView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int poit,
					long arg3) {
				// TODO Auto-generated method stub
				
				if(!HttpUtil.isNetWorkConnected(SelectActivity.this))
				{
					Toast.makeText(SelectActivity.this, "网络不通！", Toast.LENGTH_LONG).show();
				}else {
					String cityId=cityList.get(poit).getId();
					Log.d("cityId:", cityId);
					//new TcSceneryServices().getSceneryList(cityId);
					//new SceneryServices().getSceneryList("133");
					Intent it=new Intent();
					it.setClass(SelectActivity.this, SceneryActivity.class);
					startActivity(it);
				}
				
			}

			
			
		});
		
		 new LoginAsyncTask().execute(0);
		
		 
	}
	class LoginAsyncTask extends AsyncTask<Integer,Integer,Integer>{
		int type;//
		ProgressDialog dialog=new ProgressDialog(SelectActivity.this);
		
		
		@Override
		protected Integer doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			//return UserServices.login(user, pwd);
			
			//cityList=new TcSceneryServices().getCityListByProvinceId();
			cityList=new SceneryServices("","",SelectActivity.this).getCityListByProvinceId();
			Loger.d(tag, cityList.toArray().toString());
			return 0;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.setMessage("加载数据中");
			dialog.show();
		}
		@Override
		protected void onPostExecute(Integer result) 
		{
			
			Log.d(tag, "onPostExecute:"+result);
			dialog.dismiss();
			Toast.makeText(SelectActivity.this, "成功", Toast.LENGTH_LONG).show();
			ctiyListView.refresh(cityList);
//			switch(result)
//			{
//				case 0:
//					Toast.makeText(SelectActivity.this, "成功", Toast.LENGTH_LONG).show();
//					
//					break;
//				case 1:
//					Toast.makeText(SelectActivity.this, "连接不到服务器", Toast.LENGTH_LONG).show();
//					break;
//				case 2:
//					Toast.makeText(SelectActivity.this, "异常", Toast.LENGTH_LONG).show();
//					break;
//			}
		}
		
	};
}
